package cn.itcast.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

        // 1 获取安全数据的密码
        String data_password =(String)info.getCredentials();

        // 2 获取页面数据
        UsernamePasswordToken uptoken=(UsernamePasswordToken)token;
        String ym_password = new String(uptoken.getPassword()); //22f1719dfc7634fde662a6c0c43ac6a4
        //3 将页面数据做加密(shiro加密)
        Md5Hash md5Hash = new Md5Hash(ym_password,uptoken.getUsername(),1);
        String ym_md5_password = md5Hash.toString(); //22f1719dfc7634fde662a6c0c43ac6a4
        System.out.println(ym_md5_password);
        //4 返回比较结果
                    //结果不一致 false--shiro--抛异常

        return data_password.equals(ym_md5_password);
    }
}
